package main.Q1201_1300;

import java.util.*;

public class Q1281_1290 {
    public static void main(String[] args) {
        System.out.println("Question1281：整数的各未积和之差");
        System.out.println("Question1282：用户分组");
        System.out.println("Question1283：使结果不超过阈值的最小除数");
        System.out.println("Question1284：");
        System.out.println("Question1285：");
        System.out.println("Question1286：");
        System.out.println("Question1287：");
        System.out.println("Question1288：");
        System.out.println("Question1289：");
        System.out.println("Question1290：二进制链表转整数");
    }
}

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

class Question1281{
    public int subtractProductAndSum(int n) {
        int multiply=1,add=0;
        char[] num=String.valueOf(n).toCharArray();
        for (char c:num){
            multiply*=c-'0';
            add+=c-'0';
        }
        return multiply-add;
    }
}

class Question1282 {
    public List<List<Integer>> groupThePeople(int[] groupSizes) {
        List<List<Integer>> result = new ArrayList<>();
        Map<Integer,List<Integer>> map = new HashMap<>();
        for (int i = 0; i < groupSizes.length; i++) {
            int num = groupSizes[i];
            if (map.containsKey(num)) {//存在当前大小的组
                List<Integer> temp = map.get(num);
                temp.add(i);
                if (temp.size() == num) {//成组删除
                    result.add(temp);
                    map.remove(num);
                }
            } else {
                List<Integer> temp = new ArrayList<>();
                temp.add(i);
                map.put(num,temp);
                if (temp.size() == num) {//成组删除
                    result.add(temp);
                    map.remove(num);
                }
            }
        }
        return result;
    }
}

class Question1283{
    public boolean process(int[] nums,int threshold,int divided){
        int sum=0;
        for (int num:nums){
            int temp1=num/divided;
            int temp2=num%divided==0 ? 0 : 1;
            sum+=temp1+temp2;
            if (sum>threshold) return false;
        }
        if (sum>threshold) return false;
        else return true;
    }
    public int smallestDivisor(int[] nums, int threshold) {
        Arrays.sort(nums);
        int left=1, right= nums[nums.length-1];
        while (left<right){
            int mid=left+(right-left)/2;
            if (process(nums,threshold,mid)){
                right=mid;
            }else left=mid+1;
        }
        if (nums.length==1) {
            if (nums[0]%threshold==0){
                right=nums[0]/threshold;
            }else right=nums[0]/threshold+1;
        }
        return right;
    }
}

class Question1290{
    public int getDecimalValue(ListNode head) {
        StringBuffer binary=new StringBuffer();
        while (head!=null){
            binary.append(head.val);
            head=head.next;
        }
        return Integer.parseInt(binary.toString(),2);
    }
}